package shapeCalculate;

public class Triangle implements Shape {
    private double sideA;
    private double sideB;
    private double sideC;

    public Triangle(double sideA, double sideB, double sideC) {
        if (isValidTriangle(sideA, sideB, sideC)) {
            this.sideA = sideA;
            this.sideB = sideB;
            this.sideC = sideC;
        } else {
            throw new IllegalArgumentException("三边长度不满足三角形构成条件");
        }
    }

    // 校验三边是否能构成三角形（任意两边之和大于第三边）
    private boolean isValidTriangle(double a, double b, double c) {
        return a + b > c && a + c > b && b + c > a;
    }

    @Override
    public double calculateArea() {
        // 海伦公式：S = √[p(p - a)(p - b)(p - c)] ，其中 p = (a + b + c)/2
        double p = (sideA + sideB + sideC) / 2;
        return Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
    }

    @Override
    public double calculatePerimeter() {
        return sideA + sideB + sideC;
    }

    @Override
    public String calculateType() {
        return "三角形";
    }

}
